using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace CommonImagesExample
{
   public partial class CommonImagesForm : Form
   {
      public CommonImagesForm()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         Type t = typeof(Microsoft.HomeServer.Common.Client.CommonImages);
         PropertyInfo[] piArray = t.GetProperties();

         //Load list box with all of the properties of CommonImages
         foreach (PropertyInfo pi in piArray)
         {
            listBox1.Items.Add(pi);
         }
      }

      private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
      {
         e.DrawBackground();

         PropertyInfo pi = listBox1.Items[e.Index] as PropertyInfo;
         Size imageSize = Size.Empty;

         try
         {
            object v = pi.GetValue(null, null);

            //Determine if property points to an Icon or Image and draw
            if (v is Icon)
            {
               e.Graphics.DrawIcon((Icon)v, new Rectangle(e.Bounds.Left, e.Bounds.Top, 32, 32));
               imageSize = ((Icon)v).Size;
            }
            else if (v is Image)
            {
               e.Graphics.DrawImage((Image)v, new Rectangle(e.Bounds.Left, e.Bounds.Top, 32, 32));
               imageSize = ((Image)v).Size;
            }
         }
         catch
         {

         }
         
         string displayName = string.Format("{0} ({1}x{2})", pi.Name, imageSize.Height, imageSize.Width);
         SizeF stringSize = e.Graphics.MeasureString(displayName, listBox1.Font);
         
         Rectangle r = new Rectangle(e.Bounds.Left + 32, e.Bounds.Top + ((e.Bounds.Height - (int)stringSize.Height) / 2), e.Bounds.Width - 32, 32);
         
         //Display size of image
         e.Graphics.DrawString(displayName, listBox1.Font, new SolidBrush(Color.Black), r);
      }

      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
         PropertyInfo pi = listBox1.SelectedItem as PropertyInfo;

         try
         {
            object v = pi.GetValue(null, null);

            if (v is Image)
            {
               pictureBox1.Image = (Image)v;
            }
            else if (v is Icon)
            {
               pictureBox1.Image = ((Icon)v).ToBitmap();
            }
         }
         catch
         {
            pictureBox1.Image = null;
         }
      }
   }
}